package com.pompom.matchcrawler;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

public class ManipulaHttp2 {
	
	private static long apiCalls=0;
	private static long lastCount=0;
	public static String executaConsulta(String url, String prxHost, int prxPort, String prxUsr, String prxPwd) throws  IOException {
		return executaConsulta(url, prxHost, prxPort, prxUsr, prxPwd, true);
	}
	
	public static String executaConsulta(String url, String prxHost, int prxPort, String prxUsr, String prxPwd, boolean useSystemPropertiesProxyConfiguration, String[] authPolicies) throws  IOException {
		return executaConsulta(url, prxHost, prxPort, prxUsr, prxPwd, useSystemPropertiesProxyConfiguration, authPolicies, false);
	}
	
	public static String executaConsulta(String url, String prxHost, int prxPort, String prxUsr, String prxPwd, boolean useSystemPropertiesProxyConfiguration, String[] authPolicies, boolean setAuthenticationPreemptive) throws  IOException {
		apiCalls++;
		
		if (System.currentTimeMillis()-lastCount>10*60*1000)
		{
			long apiP10 = apiCalls/10;
			System.out.println("APM: "+apiP10);
			apiCalls =0;
			lastCount= System.currentTimeMillis();
		} 
		
		HttpClient httpClient = new HttpClient();
		
		httpClient.getParams().setSoTimeout(60000);
		
		GetMethod httpget = new GetMethod(url);
		
		if(useSystemPropertiesProxyConfiguration && url != null && prxHost == null) {
			String proxyHostPropertyKey, proxyPortPropertyKey;
			if(url.startsWith("https")) {
				proxyHostPropertyKey = "https.proxyHost";
				proxyPortPropertyKey = "https.proxyPort";
			} else {
				proxyHostPropertyKey = "http.proxyHost";
				proxyPortPropertyKey = "http.proxyPort";
			}
			
			prxHost = System.getProperty(proxyHostPropertyKey);
			String port = System.getProperty(proxyPortPropertyKey);
			if(port != null && port.matches("\\d+")) {
				prxPort = Integer.parseInt(port);
			}
		}
		
		if(prxHost != null && prxPort != -1) {
			httpClient.getHostConfiguration().setProxy(prxHost, prxPort);
			if(prxUsr != null && prxPwd != null) {
				HttpClientParams params = httpClient.getParams();
				if(setAuthenticationPreemptive) {
					params.setAuthenticationPreemptive(true);
				}
				if(authPolicies != null) {
					params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, Arrays.asList(authPolicies));
				}
				
				Credentials credentials = new UsernamePasswordCredentials(prxUsr, prxPwd);
				httpClient.getState().setProxyCredentials(new AuthScope(prxHost, prxPort), credentials);
			}
		}
		
		/*int responseCode = */httpClient.executeMethod(httpget);
//		return httpget.getResponseBodyAsString();
		InputStream inputStream = httpget.getResponseBodyAsStream();
		
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		StringBuffer newData = new StringBuffer();
		try{
			char[] txt = new char[1024];
			
			int count = br.read(txt);
			while ( count > 0){
				newData.append(new String(txt,0,count));
				count = br.read(txt);
			}
		}finally{
			br.close();
		}
		return newData.toString();
	}
	
	public static String executaConsulta(String url) throws  IOException {
		return executaConsulta(url, null, null);
	}
	
	public static String executaConsulta(String url, String usr, String pwd) throws  IOException {
		return executaConsulta(url, null, -1, usr, pwd);
	}
	
	public static String executaConsulta(String url, String prxHost, int prxPort, String prxUsr, String prxPwd, boolean useSystemPropertiesProxyConfiguration) throws  IOException {
		return  executaConsulta(url, prxHost, prxPort, prxUsr, prxPwd, useSystemPropertiesProxyConfiguration, null);
	}
}
